package com.sinosoft.perfect.system._authorization.action;

import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.common.system.BaseAction;
import com.sinosoft.perfect.system._.po.TRoleBusiness;
import com.sinosoft.perfect.system._.po.TUserRolebusinessRelation;
import com.sinosoft.perfect.system._authorization.service.IUserRolebusinessAuthorizeService;
import com.sinosoft.perfect.system._roleBusiness.service.IRoleBusinessService;

@SuppressWarnings("serial")
public class UserRolebusinessBindingAction extends BaseAction {

	private IUserRolebusinessAuthorizeService iuserRolebusinessService;

	private IRoleBusinessService roleBusinessService;

	private TRoleBusiness trolebusiness;

	private TUserRolebusinessRelation tuserrolebusinessrelation;

	private String unitId;

	private String userId;

	private String type; // 单位页查询or用户页查询

	public String roleBusinessQuery() {
		try {
			List<TRoleBusiness> roleBusinesses = roleBusinessService.selectByTRoleBusiness(trolebusiness, 0, 100);
			request.setAttribute("roleBusinesses", roleBusinesses);
			request.setAttribute("unitId", unitId);
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "unit".equals(type) ? roleBusinessUnit() : roleBusinessUser();
	}

	public String roleBusinessUnit() {
		try {
			List<TRoleBusiness> roleBusinesses = roleBusinessService.selectByTRoleBusiness(trolebusiness != null ? trolebusiness : new TRoleBusiness(), 0, 100);
			request.setAttribute("roleBusinesses", roleBusinesses);
			request.setAttribute("unitId", unitId);
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "user_rolebusiness_bindunit";
	}

	public String roleBusinessUser() {
		try {
			List<TUserRolebusinessRelation> userRoleBusinesses = iuserRolebusinessService.queryRolebusinessOfUser(userId);
			StringBuffer userRoleBusinessIds = new StringBuffer();
			if (userRoleBusinesses != null && userRoleBusinesses.size() != 0) {
				for (int i = 0; i < userRoleBusinesses.size(); i++) {
					userRoleBusinessIds.append(userRoleBusinesses.get(i).getRoleBusinessId()).append(",");
				}
			}
			request.setAttribute("userRoleBusinessIds", userRoleBusinessIds.toString());

			List<TRoleBusiness> roleBusinesses = roleBusinessService.selectByTRoleBusiness(trolebusiness != null ? trolebusiness : new TRoleBusiness(), 0, 100);
			StringBuffer roleBusinessIds = new StringBuffer();
			if (roleBusinesses != null && roleBusinesses.size() != 0) {
				for (int i = 0; i < roleBusinesses.size(); i++) {
					roleBusinessIds.append(roleBusinesses.get(i).getRoleBusinessId()).append(",");
				}
			}
			request.setAttribute("roleBusinessIds", roleBusinessIds.toString());
			request.setAttribute("roleBusinesses", roleBusinesses);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "user_rolebusiness_binduser";
	}

	/**
	 * 
	 */

	public IUserRolebusinessAuthorizeService getIuserRolebusinessService() {
		return iuserRolebusinessService;
	}

	public void setIuserRolebusinessService(IUserRolebusinessAuthorizeService iuserRolebusinessService) {
		this.iuserRolebusinessService = iuserRolebusinessService;
	}

	public IRoleBusinessService getRoleBusinessService() {
		return roleBusinessService;
	}

	public void setRoleBusinessService(IRoleBusinessService roleBusinessService) {
		this.roleBusinessService = roleBusinessService;
	}

	/**
	 * 
	 */

	public TRoleBusiness getTrolebusiness() {
		return trolebusiness;
	}

	public void setTrolebusiness(TRoleBusiness trolebusiness) {
		this.trolebusiness = trolebusiness;
	}

	public TUserRolebusinessRelation getTuserrolebusinessrelation() {
		return tuserrolebusinessrelation;
	}

	public void setTuserrolebusinessrelation(TUserRolebusinessRelation tuserrolebusinessrelation) {
		this.tuserrolebusinessrelation = tuserrolebusinessrelation;
	}

	public String getUnitId() {
		return unitId;
	}

	public void setUnitId(String unitId) {
		this.unitId = unitId;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
